package com.web.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.web.model.TDict;
import com.web.model.TWinNum;
import com.web.service.TDictService;

@Component("sys")
public class Sys {

    @Autowired
    private TDictService dictService;

    private Map<String, TDict> dictMap = new HashMap<String, TDict>();

    private static long lastTime = 0;// 上一次开奖时间

    private static long lastTermcd = 0;

    private static TWinNum lastWinNum;

    private static Calendar cal;

    private static SimpleDateFormat dayFmarter = new SimpleDateFormat("yyyy-MM-dd");

    private StringBuilder context = new StringBuilder();
    private static List<TDict> topDictList;

    // 默认要执行的方法
    @PostConstruct
    public void init() {
        // 初始字典缓存
        loadDicts();
        // 两面长龙排行榜
        loadTerm();
    }

    // 将dict封装到map里面
    public void loadDicts() {
        List<TDict> dictList = dictService.selectList(null);
        for (TDict d : dictList) {
            dictMap.put(d.getDictType() + "-" + d.getDictCode(), d);
        }
    }

    public TDict getDict(TDict d) {
        return dictMap.get(d.getDictType() + "-" + d.getDictCode());
    }

    public void loadTerm() {
        Map<String, Object> map = new HashMap<String, Object>();
        /** ****两面长龙*********** */
        Calendar cal = Calendar.getInstance();
        int currH = cal.get(Calendar.HOUR_OF_DAY);
        String date = dayFmarter.format(cal.getTime());
        if (currH < 9) { // 没结果时间段
            cal.add(Calendar.DATE, -1);
            date = dayFmarter.format(cal.getTime());
        }
        map.clear();
        map.put("dateStart", date + " 00:00:00");
        map.put("dateEnd", date + " 23:59:59");
        topDictList = dictService.doubleLoong(map);
    }

    public TDict getDict(String key) {
        return dictMap.get(key);
    }

    public void setTime() {
        lastTime = System.currentTimeMillis();
    }

    public void setTime(Date openTime) {
        try {
            // System.out.println("setTime :" + openTime);
            lastTime = openTime.getTime();
        } catch (Exception e) {
            // System.out.println("设置开奖时间失败");
        }

    }

    /* 上期开奖期次 */
    public void setLastTermcd(long termcd) {
        lastTermcd = termcd;
    }

    /* 距离下次开奖时间毫秒数 */
    public long getDistanceOpenTime() {
        cal = Calendar.getInstance();
        if (cal.get(Calendar.HOUR_OF_DAY) == 9 && cal.get(Calendar.MINUTE) < 7) {
            // 时间在9：00到9:07分之间，把下一次开奖时间设置到9:07
            cal.set(Calendar.MINUTE, 7);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
        } else {
            cal.setTimeInMillis(lastTime);
            cal.add(Calendar.MINUTE, 5);
        }
        return cal.getTimeInMillis() - System.currentTimeMillis() < 0 ? 0
                : cal.getTimeInMillis() - System.currentTimeMillis();
    }

    /* 封盘时间 */
    public long getLockTime() {
        cal = Calendar.getInstance();
        cal.setTimeInMillis(lastTime);
        cal.add(Calendar.MINUTE, 3);
        cal.add(Calendar.SECOND, 35);
        return cal.getTimeInMillis();
    }

    /* 距离下次封盘时间毫秒数 */
    public long getDistanceSealTime() {
        cal = Calendar.getInstance();
        if (cal.get(Calendar.HOUR_OF_DAY) == 9 && cal.get(Calendar.MINUTE) < 7) {
            // 时间在9：00到9:07分之间，把下一次封盘时间设置为9:05:40
            cal.set(Calendar.MINUTE, 5);
            cal.set(Calendar.SECOND, 20);
            cal.set(Calendar.MILLISECOND, 0);
        } else {
            cal.setTimeInMillis(lastTime);
            cal.add(Calendar.MINUTE, 3);
            cal.add(Calendar.SECOND, 25);
        }
        return (cal.getTimeInMillis() - System.currentTimeMillis()) < 0 ? 0
                : cal.getTimeInMillis() - System.currentTimeMillis();
    }

    /* 获取最后一次开奖期次 */
    public long getLastTermcd() {
        return lastTermcd;
    }

    /* 下次开奖期次 */
    public long getNextTermcd() {
        return lastTermcd + 1;
    }

    public TWinNum getLastWinNum() {
        return lastWinNum;
    }

    public void setLastWinNum(TWinNum winNum) {
        lastWinNum = winNum;
    }

    /* 购买资金记录备注 */
    public String getBuyBalanceContext(int way, long termCd, String dictType, String dictCode) {
        context.delete(0, context.length());
        context.append("投注").append(termCd).append("期产品:").append(getDict(dictType + "-" + dictCode).getDictName());
        return context.toString();
    }

    public String getOpenBalanceContext(int way, long termCd, String dictType, String dictCode) {
        context.delete(0, context.length());
        context.append("购买").append(termCd).append("期产品:").append(getDict(dictType + "-" + dictCode).getDictName())
                .append("收益");
        return context.toString();
    }

    public static List<TDict> getTopDictList() {
        return topDictList;
    }

    /* 12点导9点之间不开门 */
    public boolean isOpen() {
        boolean isOpen = true;
        cal = Calendar.getInstance();
        int currH = cal.get(Calendar.HOUR_OF_DAY);
        if (currH >= 0 && currH < 9) { // 没结果时间段
            isOpen = false;
        }
        return isOpen;
    }

}